package com.example.hp.storeapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.hp.storeapp.Activity.ShopDetailActivity;
import com.example.hp.storeapp.R;
import com.example.hp.storeapp.base.BaseActivity;
import com.example.hp.storeapp.bean.ShopResult;
import com.example.hp.storeapp.fragment.ViewHolder.ShopItemViewHolder;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import static android.media.CamcorderProfile.get;

/**
 * Created by NEUSOFT on 2017/5/3.
 */

public class ShopListAdapter extends RecyclerView.Adapter {

    private List<ShopResult> shopList = new ArrayList<>();
    public String imageUrl;
    private Context context;
    private LayoutInflater layoutInflater;

    public ShopListAdapter(Context context,List<ShopResult> shopList){

        this.context=context;
        layoutInflater = LayoutInflater.from(context);
        this.shopList.addAll(shopList);

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = layoutInflater.inflate(R.layout.item_shop,parent,false);

        ShopItemViewHolder holder = new ShopItemViewHolder(itemView);


        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        ShopItemViewHolder shopItemViewHolder = (ShopItemViewHolder) holder;

        final ShopResult shopResult = shopList.get(position);

        final String shopName =shopResult.getShopname();

        String address = shopResult.getAddress();
        String pic = shopResult.getPic();

        int level =shopResult.getLevel();
        int shopId = shopResult.getShop_id();

        if (!pic.equals("")){
//            imageUrl = " http://172.24.10.130:8080/foodService/"+shopResult.getPic();
            Picasso.with(context).load(pic).into(shopItemViewHolder.imageViews);

        }
        shopItemViewHolder.shopNameView.setText(shopName);
        shopItemViewHolder.textView_Adress.setText(address);
        shopItemViewHolder.textView_Level.setText(String.valueOf(level));

        //.....
        shopItemViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //启动新的activity(店铺详情(菜品列表))
                Toast.makeText(context,"店铺详情"+ position, Toast.LENGTH_SHORT).show();


                Intent intent = new Intent(context, ShopDetailActivity.class);

                intent.putExtra("shop_id",shopResult.getShop_id());
                intent.putExtra("shop_name",shopName);
                BaseActivity.phonenum = shopResult.getPhonenum();
                context.startActivity(intent);


            }
        });

    }

    @Override
    public int getItemCount() {

        return shopList.size();
    }
}
